﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO.Ports;
using ParkingSystem;
using ParkingSystem.RxPacket;
using ParkingSystem.SendPacket;

namespace ParkImageSBL
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    /// 

    public partial class MainWindow : Window
    {
        public CurrentBaudRate CurrentBaudRate { get; set; }    // 当前波特率
        public CurrentPort CurrentPort { get; set; }            // 当前的串口号
        public List<Port> PortList { get; set; }                // 串口Port名列表
        static ParkingSerialPort sp;

        public MainWindow()
        {
            CurrentBaudRate = new CurrentBaudRate();
            CurrentPort = new CurrentPort();
            // 动态生成串口Port名列表
            string[] ports = SerialPort.GetPortNames();         // 获取目前可用的Port.
            if(ports.Length!=0)
                CurrentPort.Port = ports[0];
            PortList = new List<Port>();
            foreach (string port in ports)
            {
                PortList.Add(new Port(port));
            }

            DataContext = this;                                 // 设定相应XMAL的上下文

            ParkingOriginalPacket.EvProcessHandShakeRsp += sp_EvProcessHandShakeRsp;
            ParkingOriginalPacket.EvProcessEnableCmdRsp += sp_EvProcessEnableCmdRsp;
            ParkingOriginalPacket.EvProcessFlashReadRsp += sp_EvProcessFlashReadRsp;
            ParkingOriginalPacket.EvProcessFlashWriteRsp += sp_EvProcessFlashWriteRsp;

            InitializeComponent();

        }

        private void OnOpenPort(object sender, RoutedEventArgs e)
        {
            try
            {
                sp = new ParkingSerialPort(CurrentPort.Port, CurrentBaudRate.Rate);
                sp.Open();   // Open the serial port;

                OpenPortBtn.IsEnabled = false;
                ClosePortBtn.IsEnabled = true;
            }
            catch (Exception ipexp)
            {
                MessageBox.Show(ipexp.Message);
            }
        }

        void sp_EvProcessHandShakeRsp(object sender, HandShakeRsp handshakersp)
        {
            MessageBox.Show("HAND SHAKE RSP");
        }

        void sp_EvProcessEnableCmdRsp(object sender, EnableCmdRsp enablcmdrsp)
        {
            MessageBox.Show("ENABLE RSP");
        }

        void sp_EvProcessFlashReadRsp(object sender, FlashReadRsp flashreadrsp)
        {
            MessageBox.Show("FLASH READ RSP");
        }

        void sp_EvProcessFlashWriteRsp(object sender, FlashWriteRsp flashwritersp)
        {
            MessageBox.Show("FLASH WRITE RSP");
        }

        private void OnClosePort(object sender, RoutedEventArgs e)
        {
            sp.Close();                     // Close serial port.
            OpenPortBtn.IsEnabled = true;
            ClosePortBtn.IsEnabled = false;
        }

        private void OnDownloadClick(object sender, RoutedEventArgs e)
        {
            try
            {
                HandShake handshake;
                sp.SendHandShake(handshake);
                

            }
            catch (Exception ipexp)
            {
                MessageBox.Show(ipexp.Message);
            }
        }
    }
}
